package com.tencent.wxcloudrun.service.impl;

import com.tencent.wxcloudrun.common.Result;
import com.tencent.wxcloudrun.dtos.req.JobsAddReq;
import com.tencent.wxcloudrun.dtos.req.PageReqDTO;
import com.tencent.wxcloudrun.dtos.resp.JobsResp;
import com.tencent.wxcloudrun.entity.JobsDO;
import com.tencent.wxcloudrun.exception.CommonErrorEnum;
import com.tencent.wxcloudrun.mapper.JobsMapper;
import com.tencent.wxcloudrun.service.JobsService;
import com.tencent.wxcloudrun.utils.ResultUtil;
import com.tencent.wxcloudrun.vos.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 工作表(目前针对兼职字段未详细划分)(Jobs)表服务实现类
 */
@Service
@Slf4j
//public class JobsServiceImpl extends ServiceImpl<JobsMapper, JobsDO> implements JobsService {
public class JobsServiceImpl implements JobsService {
    @Resource
    private JobsMapper jobsMapper;

    @Override
    public Result<PageVO<JobsResp>> getPage(PageReqDTO pageReqDTO) {
        return queryPage(pageReqDTO,null);
    }

    @Override
    public Result<PageVO<JobsResp>> getPageForRelease(PageReqDTO pageReqDTO) {
        return queryPage(pageReqDTO,3);
    }

    @Override
    public Result<Object> addJob(JobsAddReq req) {
        JobsDO entity = new JobsDO();
        BeanUtils.copyProperties(req,entity);
        entity.setExpiredDate(Objects.isNull(req.getExpiredDate())?0:req.getExpiredDate());
        jobsMapper.insert(entity);
        return ResultUtil.successData(entity);
    }

    @Override
    public Result<Object> updateJobState(Long id, Integer state) {
        JobsDO data = jobsMapper.selectById(id);
        if(Objects.isNull(data)){
            return ResultUtil.error(CommonErrorEnum.DATA_NOT_EXISTS);
        }
        data.setState(state);
        jobsMapper.updateById(data);
        return ResultUtil.success();
    }

    private Map<String, Object> convertPageParameter(PageReqDTO pageReqDTO,Integer state){
        Map<String, Object> params = new HashMap<>();
        params.put("offset", (pageReqDTO.getPage() - 1) * pageReqDTO.getPageSize());
        params.put("pageSize", pageReqDTO.getPageSize());
        if(Objects.nonNull(state)){
            params.put("state", state);
        }
        return params;
    }

    private Result<PageVO<JobsResp>> queryPage(PageReqDTO pageReqDTO,Integer state){
        Map<String, Object> params = convertPageParameter(pageReqDTO,state);
        Integer total = jobsMapper.selectPageCount(params);
        List<JobsDO> page = jobsMapper.selectPage(params);
        List<JobsResp> resps = new ArrayList<>();
        if(!CollectionUtils.isEmpty(page)){
            page.forEach(item -> {
                JobsResp resp = new JobsResp();
                BeanUtils.copyProperties(item,resp);
                resps.add(resp);
            });
        }
        PageVO pageVO = new PageVO<>();
        pageVO.setTotal(total);
        pageVO.setRecords(resps);
        pageVO.setPageNum(pageVO.getPageNum());
        pageVO.setPageSize(pageVO.getPageSize());
        return ResultUtil.successData(pageVO);
    }

}

